// $codepro.audit.disable
/**
 * License under GNU's GPL 
 * AdminGui.java 
 */

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import java.util.*;
/**
 * The GUI for the Admin class
 * 
 * @author Team 9
 *
 */
public class AdminGui extends JTabbedPane {
	public JTabbedPane tp;
	
	private JFrame frame;
	
	private JButton suspend, unsuspend, edit, saveChanges, delete;
	
	private String editL, editF, editU, editP;
	
	private int editI;
	
	private JButton btnView, btnSaveOrders;
	
	private JTextField editUName, editFirst, editLast, editPass;
	
	private JTextField fname, lname, uname, pass;
	
	private JComboBox types, types1;
	
	private JList list, listSuspended;
	
	private JLabel message, tRMessage;
	
	private JLabel label_5, label_6, label_7;
	
	private JScrollPane activeUsers, rp;
	
	private JList pList;
	
	private JScrollPane accountPane;
	
	private ArrayList<String> suspended = new ArrayList<String>();
	
	private ArrayList<String> active = new ArrayList<String>();
	
	private ArrayList<String> adminP = new ArrayList<String>();
	
	private JTextField textField;
	
	private JTextField textField_1;
	
	private JTextField textField_2;
	
	private JTextField textField_3;
	
	JTextPane textPane, textPane_1, textPane_2;
	
	JCheckBox checkBox, checkBox_1, checkBox_2, checkBox_3, checkBox_4,
	
			checkBox_5, checkBox_6, checkBox_7, checkBox_8, checkBox_9;
	
	private JLabel lblMessage;
	
	private JLabel label_19;
	
	private JTextArea textArea;
	/**
	 * Constructs a AdminGui object
	 */
	public AdminGui() {
		initialize();
	}
	/**
	 * Goes through all the text files and compiles a list of active users
	 * @throws FileNotFoundException
	 */
	// goes through all the text files and complies a list of active..bitches!
	public void sort() throws FileNotFoundException {
		String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
		for (int i = 0; i < 4; i++) {
			Scanner fileScan = new Scanner(new File(types[i] + ".txt"));
			fileScan.useDelimiter(" ");
			while (fileScan.hasNext()) {
				String first = fileScan.next();
				String last = fileScan.next();
				fileScan.next();
				fileScan.next();
				String act = fileScan.next();
				fileScan.nextLine();
				if (act.equals("1")) {
					active.add(last + ", " + first);
				} else if (act.equals("0")) {
					suspended.add(last + ", " + first);
				}
			}
		}
		Collections.sort(active);
		Collections.sort(suspended);
	}
	/**
	 * A second sorting method for Users
	 * @throws FileNotFoundException
	 */
	public void sort2() throws FileNotFoundException {
		Scanner fileScan = new Scanner(new File("Patient.txt"));
		fileScan.useDelimiter(" ");
		while (fileScan.hasNext()) {
			String first = fileScan.next();
			String last = fileScan.next();
			fileScan.next();
			fileScan.next();
			fileScan.nextLine();
			adminP.add(last + ", " + first);
		}
		Collections.sort(adminP);
	}
	/**
	 * Creates the GUI. The constructor calls this method
	 */
	public void initialize() {
		frame = new JFrame("Admin");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(800, 600));

		// 6 main tabs
		JPanel patients = new JPanel();
		JPanel appointment = new JPanel();
		JPanel logout = new JPanel();
		JPanel main = new JPanel();
		JPanel accounts = new JPanel();

		// What appears on main
		// ------------------------

		// What appears on appointments
		// ------------------------

		// What appears on patients
		// ------------------------

		// What appears on reports
		// ------------------------

		// What appears on accounts
		// This part is the upper section
		JPanel btnHolder = new JPanel();
		JButton users = new JButton("Users");
		JButton pw = new JButton("Passwords");
		JButton spsns = new JButton("Suspensions");
		btnHolder.add(users);
		btnHolder.add(pw);
		btnHolder.add(spsns);
		// -----------------------

		// top Left section
		JPanel topLeft = new JPanel();
		JPanel tLblank1 = new JPanel();
		JLabel acnt = new JLabel("Select Account");
		try {
			sort();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		list = new JList(active.toArray());
		activeUsers = new JScrollPane(list);
		topLeft.setLayout(new GridLayout(2, 2));
		topLeft.add(tLblank1);
		tLblank1.add(acnt);
		topLeft.add(activeUsers);
		topLeft.add(new JLabel());
		// ---------------------

		// top right section
		String[] roar1 = { "Patient", "Nurse", "Doctor", "Admin" };
		JPanel topRight = new JPanel();
		edit = new JButton("Edit");
		edit.setBounds(10, 5, 73, 23);
		edit.addActionListener(new editListener());
		suspend = new JButton("Suspend");
		suspend.setBounds(88, 5, 96, 23);
		suspend.addActionListener(new suspListener());
		delete = new JButton("Delete");
		delete.setBounds(42, 5, 86, 23);
		delete.addActionListener(new deleteListener());
		JPanel editHolder = new JPanel();
		editHolder.setBounds(0, 0, 194, 44);
		JPanel deleteHolder = new JPanel();
		deleteHolder.setBounds(194, 0, 194, 44);
		editHolder.setLayout(null);
		editHolder.add(edit);
		editHolder.add(suspend);
		deleteHolder.setLayout(null);
		deleteHolder.add(delete);
		topRight.setLayout(null);
		topRight.add(editHolder);
		topRight.add(deleteHolder);
		JPanel labelHolder = new JPanel();
		labelHolder.setBounds(0, 55, 388, 167);
		labelHolder.setLayout(null);
		JLabel label = new JLabel("Edit First Name");
		label.setBounds(0, 3, 194, 14);
		labelHolder.add(label);
		JLabel label_1 = new JLabel("Edit Last Name");
		label_1.setBounds(0, 30, 194, 14);
		labelHolder.add(label_1);
		JLabel label_2 = new JLabel("Edit Username");
		label_2.setBounds(0, 56, 194, 14);
		labelHolder.add(label_2);
		JLabel label_3 = new JLabel("Edit Password");
		label_3.setBounds(0, 84, 194, 14);
		labelHolder.add(label_3);
		JLabel label_4 = new JLabel("Change Access");
		label_4.setBounds(0, 112, 194, 14);
		labelHolder.add(label_4);
		topRight.add(labelHolder);
		saveChanges = new JButton("Save Changes");
		saveChanges.setBounds(232, 140, 156, 23);
		labelHolder.add(saveChanges);
		types1 = new JComboBox(roar1);
		types1.setBounds(194, 109, 194, 20);
		labelHolder.add(types1);
		editPass = new JTextField("Edit Pass");
		editPass.setBounds(194, 81, 194, 20);
		labelHolder.add(editPass);
		editFirst = new JTextField("Edit First Name");
		editFirst.setBounds(194, 0, 194, 20);
		labelHolder.add(editFirst);
		editLast = new JTextField("Edit Last Name");
		editLast.setBounds(194, 28, 194, 20);
		labelHolder.add(editLast);
		editUName = new JTextField("Edit Username");
		editUName.setBounds(194, 55, 194, 20);
		labelHolder.add(editUName);
		tRMessage = new JLabel("");
		tRMessage.setBounds(0, 141, 194, 14);
		labelHolder.add(tRMessage);
		saveChanges.addActionListener(new saveEditListener());

		// bottom right
		JPanel btmRight = new JPanel();
		JPanel brt = new JPanel();
		JPanel brb = new JPanel();
		JButton add = new JButton("Add");
		message = new JLabel(" ");
		add.addActionListener(new addListener());
		brb.setLayout(new GridLayout(0, 2));
		String[] roar = { "Patient", "Nurse", "Doctor", "Admin" };
		types = new JComboBox(roar);
		btmRight.add(new JLabel("Add New User"));
		fname = new JTextField(10);
		lname = new JTextField(10);
		uname = new JTextField(10);
		pass = new JTextField(10);
		btmRight.add(brt);
		btmRight.add(brb);
		brb.add(new JLabel("First Name"));
		brb.add(fname);
		brb.add(new JLabel("Last Name"));
		brb.add(lname);
		brb.add(new JLabel("User Name"));
		brb.add(uname);
		brb.add(new JLabel("Password"));
		brb.add(pass);
		brb.add(new JLabel("User Type"));
		brb.add(types);
		brb.add(add);
		brb.add(new JLabel());
		brb.add(message);

		// Bottom left
		JPanel btmLeft = new JPanel();
		JPanel btmLLeft = new JPanel();
		JPanel btmLRight = new JPanel();
		JPanel blank1 = new JPanel();
		JPanel blank2 = new JPanel();
		unsuspend = new JButton("Unsuspend");
		unsuspend.addActionListener(new unsuspListener());
		JButton activate = new JButton("Activate");
		JLabel susAcnts = new JLabel("    Suspended");
		JLabel actAcnts = new JLabel("                 Suspended");
		// listSuspended = new JList(suspended.toArray());
		// JScrollPane lp = new JScrollPane(listSuspended);
		listSuspended = new JList(suspended.toArray());
		rp = new JScrollPane(listSuspended);
		btmLLeft.setLayout(new GridLayout(2, 2));
		btmLLeft.add(susAcnts);
		// btmLLeft.add(lp);
		btmLLeft.add(new JLabel());
		btmLLeft.add(blank1);
		blank1.add(activate);
		btmLRight.setLayout(new GridLayout(2, 2));
		btmLRight.add(actAcnts);
		btmLRight.add(rp);
		btmLRight.add(new JLabel());
		btmLRight.add(blank2);
		blank2.add(unsuspend);
		btmLeft.setLayout(new GridLayout(1, 0));
		// btmLeft.add(btmLLeft);
		btmLeft.add(btmLRight);
		// ------------------------

		accounts.setLayout(new GridLayout(2, 2));
		accounts.add(topLeft);
		accounts.add(topRight);
		accounts.add(btmLeft);
		accounts.add(btmRight);
		// ------------------------

		// What appears on Logout
		JLabel orly = new JLabel("Are You Sure?");
		JButton yarly = new JButton("Yes");
		yarly.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Gui obj = new Gui();
				obj.getFrame().setVisible(true);
				setVisible(false);
				frame.setVisible(false);
				removeAll();
			}
		});
		logout.add(orly);
		logout.add(yarly);
		// ------------------------

		addTab("Main", main);

		JLabel lblNewLabel = new JLabel("Welcome Admin!");
		main.add(lblNewLabel);
		addTab("Appointments", appointment);
		appointment.setLayout(null);

		label_19 = new JLabel("Appointments for the day-");
		label_19.setBounds(306, 11, 170, 20);
		appointment.add(label_19);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setBounds(168, 32, 410, 429);
		for (int i = 0; i < Gui.getAppointArray().size(); i++) {
			textArea.append(Gui.getAppointArray().get(i).toString() + "\n");
		}
		appointment.add(textArea);
		addTab("Patients", patients);
		patients.setLayout(null);

		try {
			sort2();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pList = new JList(adminP.toArray());
		accountPane = new JScrollPane(pList);
		accountPane.setBounds(30, 36, 201, 482);
		patients.add(accountPane);

		btnView = new JButton("View");
		btnView.addActionListener(new viewListener());
		btnView.setBounds(250, 35, 89, 23);
		patients.add(btnView);

		label_5 = new JLabel("Treatment Record For Appointment on:");
		label_5.setBounds(369, 11, 400, 14);
		patients.add(label_5);

		label_6 = new JLabel("Doctor:");
		label_6.setBounds(369, 36, 300, 14);
		patients.add(label_6);

		label_7 = new JLabel("Nurse:");
		label_7.setBounds(369, 64, 300, 14);
		patients.add(label_7);

		JLabel label_8 = new JLabel("BP:");
		label_8.setBounds(369, 89, 46, 14);
		patients.add(label_8);

		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		textField.setBounds(419, 86, 86, 20);
		patients.add(textField);

		JLabel label_9 = new JLabel("Weight:");
		label_9.setBounds(573, 92, 50, 14);
		patients.add(label_9);

		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setColumns(10);
		textField_1.setBounds(683, 89, 86, 20);
		patients.add(textField_1);

		textField_2 = new JTextField();
		textField_2.setEditable(false);
		textField_2.setColumns(10);
		textField_2.setBounds(683, 112, 86, 20);
		patients.add(textField_2);

		JLabel label_10 = new JLabel("Tempurature:");
		label_10.setBounds(573, 115, 100, 14);
		patients.add(label_10);

		textField_3 = new JTextField();
		textField_3.setEditable(false);
		textField_3.setColumns(10);
		textField_3.setBounds(419, 114, 86, 20);
		patients.add(textField_3);

		JLabel label_11 = new JLabel("Height:");
		label_11.setBounds(369, 115, 46, 14);
		patients.add(label_11);

		JLabel label_12 = new JLabel("Chief Complaints:");
		label_12.setBounds(369, 143, 136, 14);
		patients.add(label_12);

		textPane = new JTextPane();
		textPane.setText(" ");
		textPane.setEditable(false);
		textPane.setBounds(369, 162, 400, 40);
		patients.add(textPane);

		JLabel label_13 = new JLabel("Diagnosis:");
		label_13.setBounds(369, 213, 89, 14);
		patients.add(label_13);

		textPane_1 = new JTextPane();
		textPane_1.setText(" ");
		textPane_1.setEditable(false);
		textPane_1.setBounds(369, 233, 400, 40);
		patients.add(textPane_1);

		JLabel label_14 = new JLabel("Doctor's Orders-");
		label_14.setBounds(369, 296, 108, 14);
		patients.add(label_14);

		JLabel label_15 = new JLabel("Lab Work:");
		label_15.setBounds(369, 321, 89, 14);
		patients.add(label_15);

		checkBox = new JCheckBox("Blood Test");
		checkBox.setEnabled(false);
		checkBox.setBounds(434, 317, 100, 23);
		patients.add(checkBox);

		checkBox_1 = new JCheckBox("X-Ray");
		checkBox_1.setEnabled(false);
		checkBox_1.setBounds(536, 317, 100, 23);
		patients.add(checkBox_1);

		checkBox_2 = new JCheckBox("MRI");
		checkBox_2.setEnabled(false);
		checkBox_2.setBounds(638, 317, 100, 23);
		patients.add(checkBox_2);

		checkBox_3 = new JCheckBox("Throat Culture");
		checkBox_3.setEnabled(false);
		checkBox_3.setBounds(434, 342, 144, 23);
		patients.add(checkBox_3);

		checkBox_4 = new JCheckBox("Lumbar Puncture");
		checkBox_4.setEnabled(false);
		checkBox_4.setBounds(638, 342, 158, 23);
		patients.add(checkBox_4);

		JLabel label_16 = new JLabel("Prescriptions:");
		label_16.setBounds(369, 370, 108, 14);
		patients.add(label_16);

		checkBox_5 = new JCheckBox("Vicodin");
		checkBox_5.setEnabled(false);
		checkBox_5.setBounds(638, 391, 97, 23);
		patients.add(checkBox_5);

		checkBox_6 = new JCheckBox("Zyrtec");
		checkBox_6.setEnabled(false);
		checkBox_6.setBounds(536, 366, 97, 23);
		patients.add(checkBox_6);

		checkBox_7 = new JCheckBox("Prozac");
		checkBox_7.setEnabled(false);
		checkBox_7.setBounds(638, 366, 97, 23);
		patients.add(checkBox_7);

		checkBox_8 = new JCheckBox("Ibuprofen");
		checkBox_8.setEnabled(false);
		checkBox_8.setBounds(434, 391, 97, 23);
		patients.add(checkBox_8);

		checkBox_9 = new JCheckBox("Keflex");
		checkBox_9.setEnabled(false);
		checkBox_9.setBounds(536, 391, 97, 23);
		patients.add(checkBox_9);

		JLabel label_17 = new JLabel("Follow Up Instructions:");
		label_17.setBounds(369, 421, 150, 14);
		patients.add(label_17);

		textPane_2 = new JTextPane();
		textPane_2.setText(" ");
		textPane_2.setEditable(false);
		textPane_2.setBounds(369, 437, 400, 40);
		patients.add(textPane_2);

		btnSaveOrders = new JButton("Edit Records & Orders");
		btnSaveOrders.setBounds(575, 495, 200, 23);
		btnSaveOrders.setVisible(false);
		btnSaveOrders.addActionListener(new eRecordsListener());
		patients.add(btnSaveOrders);

		JLabel label_18 = new JLabel("Select Account");
		label_18.setBounds(30, 11, 103, 14);
		patients.add(label_18);

		lblMessage = new JLabel(" ");
		lblMessage.setBounds(369, 499, 200, 14);
		patients.add(lblMessage);
		addTab("Accounts", accounts);
		addTab("Logout", logout);

		JButton btnNewButton = new JButton("No");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				setSelectedIndex(0);
			}
		});
		logout.add(btnNewButton);

		frame.getContentPane().add(this);
		frame.pack();
		frame.setVisible(true);
	}
	/**
	 * Returns the JFrame that the GUI is on
	 * @return the JFrame
	 */
	public JFrame getFrame() {
		return frame;
	}
	/**
	 * Creates the action listener that the GUI uses
	 * @author Team 9 
	 *
	 */
	private class ButtonListener implements ActionListener {
		/**
		 * the action performed method for the button listener class
		 */
		public void actionPerformed(ActionEvent event) {
			// add code here
			// either copy/paste the entire class and just rename ButtonListener
			// or use source
			Object source = event.getSource();

			if (source == suspend)
				System.out.println("BACON");
		}
	}
	/**
	 * The add Listener class. It creates a User when called
	 * @author Team 9
	 *
	 */
	private class addListener implements ActionListener {
		/**
		 * the action performed method that the GUI objects use to make users
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			boolean full = true;
			if (fname.getText().length() == 0 || lname.getText().length() == 0
					|| uname.getText().length() == 0
					|| pass.getText().length() == 0) {
				message.setText("Please make sure all fields are completed!");
				full = false;
			} else {
				message.setText(" ");
			}
			String type = (String) types.getSelectedItem();

			boolean create = false;
			try {
				create = taken(uname.getText());
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (!create) {
				message.setText("User name is taken, please pick another one.");
			} else {
				tRMessage.setText(" ");
			}

			String fName = fname.getText().trim();
			String lName = lname.getText().trim();
			String uName = uname.getText().trim();
			String passW = pass.getText().trim();
			if (create) {
				active.add(lName + ", " + fName);
				Collections.sort(active);
				list.setListData(active.toArray());
				activeUsers.updateUI();
			}

			if (type == "Patient" && create && full) {
				// figure out who is logged in....
				String file = lName + ", " + fName + ".txt";
				File pFile = new File("Patient/" + file);

				;
				try {
					FileWriter fstream2 = new FileWriter("Patient/" + file,
							true);
					BufferedWriter out2 = new BufferedWriter(fstream2);
					out2.write("Treatment Record for Appointment on: ");
					out2.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				adminP.add(lName + ", " + fName);
				Collections.sort(adminP);
				pList.setListData(adminP.toArray());
				accountPane.updateUI();
				try {
					pFile.createNewFile();
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				new Patient(fName, lName, uName, passW);
				try {
					// Create file
					FileWriter fstream = new FileWriter("Patient.txt", true);
					BufferedWriter out = new BufferedWriter(fstream);
					out.write(fName + " " + lName + " " + uName + " " + passW
							+ " 1" + " \n");
					// Close the output stream
					out.close();
				} catch (Exception e) {// Catch exception if any
					System.err.println("Error: " + e.getMessage());
				}
			}

			if (type == "Nurse" && create && full) {
				// figure out who is logged in....
				new Nurse(fName, lName, uName, passW);
				try {
					// Create file
					FileWriter fstream = new FileWriter("Nurse.txt", true);
					BufferedWriter out = new BufferedWriter(fstream);
					out.write(fName + " " + lName + " " + uName + " " + passW
							+ " 1" + " \n");
					// Close the output stream
					out.close();
				} catch (Exception e) {// Catch exception if any
					System.err.println("Error: " + e.getMessage());
				}
			}

			if (type == "Doctor" && create && full) {
				// figure out who is logged in....
				new Doctor(fName, lName, uName, passW);
				try {
					// Create file
					FileWriter fstream = new FileWriter("Doctor.txt", true);
					BufferedWriter out = new BufferedWriter(fstream);
					out.write(fName + " " + lName + " " + uName + " " + passW
							+ " 1" + " \n");
					// Close the output stream
					out.close();
				} catch (Exception e) {// Catch exception if any
					System.err.println("Error: " + e.getMessage());
				}
			}

			if (type == "Admin" && create && full) {
				// figure out who is logged in....
				new Admin(fName, lName, uName, passW);
				try {
					// Create file
					FileWriter fstream = new FileWriter("Admin.txt", true);
					BufferedWriter out = new BufferedWriter(fstream);
					out.write(fName + " " + lName + " " + uName + " " + passW
							+ " 1" + " \n");
					// Close the output stream
					out.close();
				} catch (Exception e) {// Catch exception if any
					System.err.println("Error: " + e.getMessage());
				}
			}
		}
	}
	/**
	 * The main method that shows the admin GUI
	 * @param args
	 */
	public static void main(String[] args) {
		AdminGui admin = new AdminGui();
		admin.getFrame();
	}
	/**
	 * returns true if the user exists
	 * @param userName The user in question
	 * @return a true/false value of whether the user exists
	 * @throws FileNotFoundException 
	 */
	public boolean taken(String userName) throws FileNotFoundException {
		String tUName;
		String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
		for (int i = 0; i < 4; i++) {
			Scanner fileScan = new Scanner(new File(types[i] + ".txt"));
			while (fileScan.hasNext()) {
				fileScan.next();
				fileScan.next();
				tUName = fileScan.next();
				fileScan.nextLine();
				if (userName.equals(tUName)) {
					return false;
				}
			}
		}
		return true;
	}
	/**
	 * The action listener that edits users
	 * @author Team 9
	 *
	 */
	private class editListener implements ActionListener {
		/**
		 * the action performed method that edits a user when a GUI object calls this
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			tRMessage.setText(" ");
			String la = (String) list.getSelectedValue();
			boolean yes;
			if (la == null) {
				System.out.println("Please Select a user!");
				yes = false;
			} else {
				yes = true;
			}
			if (yes) {
				String[] tokens = la.split(", ");
				editL = tokens[0];
				editF = tokens[1]; // first and last name of the field selected
				String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
				for (int i = 0; i < 4; i++) {
					Scanner fileScan = null;
					try {
						fileScan = new Scanner(new File(types[i] + ".txt"));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					while (fileScan.hasNext()) {
						String first = fileScan.next();
						String last = fileScan.next();
						if (editF.equals(first) && editL.equals(last)) {
							editI = i;
							editFirst.setText(first);
							editLast.setText(last);
							String un = fileScan.next();
							editU = un;
							editUName.setText(un);
							String pw = fileScan.next();
							editP = pw;
							editPass.setText(pw);
							if (i == 0) {
								types1.setSelectedIndex(0);
							} else if (i == 1) {
								types1.setSelectedIndex(2);
							} else if (i == 2) {
								types1.setSelectedIndex(3);
							} else if (i == 3) {
								types1.setSelectedIndex(1);
							}
						}
						fileScan.nextLine();
					}
				}
			}
		}
	}
	/**
	 * The action listener that saves edits to users
	 * @author Team 9
	 *
	 */
	private class saveEditListener implements ActionListener {
		/**
		 * the action performed method that the GUIobject uses to save edits to users
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			File temp = new File("temp.txt");
			FileWriter fstream = null;
			try {
				fstream = new FileWriter("temp.txt");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			BufferedWriter out = new BufferedWriter(fstream);
			BufferedReader reader = null;
			String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
			try {
				File inputFile = new File(types[editI] + ".txt");
				reader = new BufferedReader(new FileReader(inputFile));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String currentLine;
			Scanner fileScan = null;
			try {
				fileScan = new Scanner(new File(types[editI] + ".txt"));
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			boolean create = true;
			if (!(editUName.getText().equals(editU))) {
				try {
					create = taken(editUName.getText());
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			if (!create) {
				tRMessage
						.setText("User name is taken, please pick another one.");
			} else {
				tRMessage.setText("Patient Edited!");
			}

			try {
				while ((currentLine = reader.readLine()) != null) {
					String first = fileScan.next();
					String last = fileScan.next();
					fileScan.nextLine();
					if (!editF.equals(first) && !editL.equals(last)) {
						try {
							out.write(currentLine);
							out.write("\n");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			if (create) {
				try {
					out.write(editFirst.getText().trim() + " "
							+ editLast.getText().trim() + " "
							+ editUName.getText().trim() + " "
							+ editPass.getText().trim() + " 1 \n");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} else {
				try {
					out.write(editF + " " + editL + " " + editU + " " + editP);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			File la = new File(types[editI] + ".txt");
			la.delete();
			File ba = new File(types[editI] + ".txt");
			temp.renameTo(ba);

			if (create) {
				editFirst.setText(" ");
				editLast.setText(" ");
				editUName.setText(" ");
				editPass.setText(" ");
			}
		}
	}
	/**
	 * An Action listener class for unsuspending users
	 * @author Team 9
	 *
	 */
	private class unsuspListener implements ActionListener {
		/**
		 * an action performed method that GUIs use to unsuspend users
		 * 
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String la = (String) listSuspended.getSelectedValue();
			boolean yes;
			if (la == null) {
				System.out.println("Please Select a user!");
				yes = false;
			} else {
				yes = true;
			}
			if (yes) {
				String[] tokens = la.split(", ");
				editL = tokens[0];
				editF = tokens[1]; // first and last name of the field selected
				int i = findType(editF, editL);
				File temp = new File("temp.txt");
				FileWriter fstream = null;
				try {
					fstream = new FileWriter("temp.txt");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedWriter out = new BufferedWriter(fstream);
				BufferedReader reader = null;
				String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
				try {
					File inputFile = new File(types[i] + ".txt");
					reader = new BufferedReader(new FileReader(inputFile));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String currentLine;
				Scanner fileScan = null;
				try {
					fileScan = new Scanner(new File(types[i] + ".txt"));
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					while ((currentLine = reader.readLine()) != null) {
						String first = fileScan.next();
						String last = fileScan.next();
						String usen = fileScan.next();
						String pw = fileScan.next();
						fileScan.nextLine();
						// System.out.println(first + " " + last);
						if (!editF.equals(first) && !editL.equals(last)) {
							try {
								out.write(currentLine);
								out.write("\n");
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						} else if (editF.equals(first) && editL.equals(last)) {
							out.write(first + " " + last + " " + usen + " "
									+ pw + " 1 \n");
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				File la1 = new File(types[i] + ".txt");
				la1.delete();
				File ba = new File(types[i] + ".txt");
				temp.renameTo(ba);

				active = new ArrayList<String>();
				suspended = new ArrayList<String>();
				try {
					sort();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				list.setListData(active.toArray());
				listSuspended.setListData(suspended.toArray());
				activeUsers.updateUI();
				rp.updateUI();
			}
		}
	}
	/**
	 * The action listener that deletes users
	 * @author Team 9
	 *
	 */
	private class deleteListener implements ActionListener {
		/**
		 * the delete method that the GUI uses
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String la = (String) list.getSelectedValue();
			boolean yes;
			if (la == null) {
				System.out.println("Please Select a user!");
				yes = false;
			} else {
				yes = true;
			}
			if (yes) {
				String[] tokens = la.split(", ");
				editL = tokens[0];
				editF = tokens[1]; // first and last name of the field selected
				int i = findType(editF, editL);
				File temp = new File("temp.txt");
				FileWriter fstream = null;
				try {
					fstream = new FileWriter("temp.txt");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedWriter out = new BufferedWriter(fstream);
				BufferedReader reader = null;
				String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
				try {
					File inputFile = new File(types[i] + ".txt");
					reader = new BufferedReader(new FileReader(inputFile));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String currentLine;
				Scanner fileScan = null;
				try {
					fileScan = new Scanner(new File(types[i] + ".txt"));
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					while ((currentLine = reader.readLine()) != null) {
						String first = fileScan.next();
						String last = fileScan.next();
						fileScan.nextLine();
						// System.out.println(first + " " + last);
						if (!editF.equals(first) && !editL.equals(last)) {
							try {
								out.write(currentLine);
								out.write("\n");
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				File la1 = new File(types[i] + ".txt");
				la1.delete();
				File ba = new File(types[i] + ".txt");
				temp.renameTo(ba);
				active = new ArrayList<String>();
				try {
					sort();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				list.setListData(active.toArray());
				activeUsers.updateUI();
			}
		}
	}
	/**
	 * An Action listener class for suspending users
	 * @author Team 9
	 *
	 */
	private class suspListener implements ActionListener {
		/**
		 * The action performed method for suspending users
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String la = (String) list.getSelectedValue();
			boolean yes;
			if (la == null) {
				System.out.println("Please Select a user!");
				yes = false;
			} else {
				yes = true;
			}
			if (yes) {
				String[] tokens = la.split(", ");
				editL = tokens[0];
				editF = tokens[1]; // first and last name of the field selected
				int i = findType(editF, editL);
				File temp = new File("temp.txt");
				FileWriter fstream = null;
				try {
					fstream = new FileWriter("temp.txt");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedWriter out = new BufferedWriter(fstream);
				BufferedReader reader = null;
				String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
				try {
					File inputFile = new File(types[i] + ".txt");
					reader = new BufferedReader(new FileReader(inputFile));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String currentLine;
				Scanner fileScan = null;
				try {
					fileScan = new Scanner(new File(types[i] + ".txt"));
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					while ((currentLine = reader.readLine()) != null) {
						String first = fileScan.next();
						String last = fileScan.next();
						String usen = fileScan.next();
						String pw = fileScan.next();
						fileScan.nextLine();
						// System.out.println(first + " " + last);
						if (!editF.equals(first) && !editL.equals(last)) {
							try {
								out.write(currentLine);
								out.write("\n");
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						} else if (editF.equals(first) && editL.equals(last)) {
							out.write(first + " " + last + " " + usen + " "
									+ pw + " 0 \n");
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				File la1 = new File(types[i] + ".txt");
				la1.delete();
				File ba = new File(types[i] + ".txt");
				temp.renameTo(ba);

				active = new ArrayList<String>();
				suspended = new ArrayList<String>();
				try {
					sort();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				list.setListData(active.toArray());
				listSuspended.setListData(suspended.toArray());
				activeUsers.updateUI();
				rp.updateUI();
			}
		}
	}
	/**
	 * An Action Listener class for viewing users
	 * @author David
	 *
	 */
	private class viewListener implements ActionListener {
    	/**
    	 * the action performed method that the objects in the GUI
    	 * use to view users
    	 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (btnView.getText().equals("View")) {
				String la = (String) pList.getSelectedValue();
				boolean yes;
				if (la == null) {
					lblMessage.setText("Please Select a user!");
					yes = false;
				} else {
					yes = true;
				}

				if (yes) {
					// btnSaveRecord.setVisible(true);
					lblMessage.setText(" ");
					Scanner fileScan = null;
					String[] tokens = la.split(", ");
					editL = tokens[0];
					editF = tokens[1]; // first and last name of the field
										// selected
					try {
						fileScan = new Scanner(new File("Patient/" + editL
								+ ", " + editF + ".txt"));
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					String he = fileScan.nextLine();
					if (he.equals("Treatment Record for Appointment on: ")) {
						label_5.setText("No Appointment on Record!");
					} else {
						btnView.setText("Done");
						btnSaveOrders.setVisible(true);
						label_5.setText(he);
						// fileScan.nextLine();
						label_6.setText(fileScan.nextLine());
						label_7.setText(fileScan.nextLine());
						textField.setText(fileScan.nextLine());
						textField_3.setText(fileScan.nextLine());
						textField_1.setText(fileScan.nextLine());
						textField_2.setText(fileScan.nextLine());
						textPane.setText(fileScan.nextLine());
						textPane_1.setText(fileScan.nextLine());
						textPane_2.setText(fileScan.nextLine());
						JCheckBox[] checkBoxes = { checkBox, checkBox_1,
								checkBox_2, checkBox_3, checkBox_4, checkBox_5,
								checkBox_6, checkBox_7, checkBox_8, checkBox_9 };
						for (int i = 0; i < checkBoxes.length; i++) {
							checkBoxes[i].setEnabled(false);
							checkBoxes[i].setSelected(fileScan.nextBoolean());
						}
					}
				}
			} else if (btnView.getText().equals("Done")) {
				btnView.setText("View");
				clear();
			}
		}

	}
	/**
	 * A method that returns the type of user found
	 * @param ffirst The user's first name
	 * @param llast The user's last name
	 * @return an int that tells you the type of user
	 */
	public int findType(String ffirst, String llast) {
		String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
		for (int i = 0; i < 4; i++) {
			Scanner fileScan = null;
			try {
				fileScan = new Scanner(new File(types[i] + ".txt"));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			while (fileScan.hasNext()) {
				String first = fileScan.next();
				String last = fileScan.next();
				if (ffirst.equals(first) && llast.equals(last)) {
					return i;
				}
				fileScan.nextLine();
			}
		}
		return 0;
	}
	/**
	 * ERecords Action Listener
	 * @author Team 9
	 *
	 */
	private class eRecordsListener implements ActionListener {
	 	/**
    	 * the action performed method for ERecords
    	 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (btnSaveOrders.getText().equals("Edit Records & Orders")) {
				textField.setEditable(true);
				textField_1.setEditable(true);
				textField_2.setEditable(true);
				textField_3.setEditable(true);
				textPane.setEditable(true);
				textPane_1.setEditable(true);
				JCheckBox[] checkBoxes = { checkBox, checkBox_1, checkBox_2,
						checkBox_3, checkBox_4, checkBox_5, checkBox_6,
						checkBox_7, checkBox_8, checkBox_9 };
				for (int i = 0; i < checkBoxes.length; i++) {
					checkBoxes[i].setEnabled(true);
				}
				textPane_2.setEditable(true);
				btnSaveOrders.setText("Save Records & Orders");
			} else if (btnSaveOrders.getText().equals("Save Records & Orders")) {
				// tring file = editL + ", " + editF + ".txt";
				FileWriter fstream = null;
				File temp = new File("Patient/temp.txt");
				try {
					fstream = new FileWriter("Patient/temp.txt", true);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedWriter out = new BufferedWriter(fstream);
				try {
					out.write(label_5.getText() + " \n");
					out.write(label_6.getText() + " \n");
					out.write(label_7.getText() + " \n");
					out.write(textField.getText() + " \n");
					out.write(textField_3.getText() + " \n");
					out.write(textField_1.getText() + " \n");
					out.write(textField_2.getText() + " \n");
					out.write(textPane.getText() + " \n");
					out.write(textPane_1.getText() + " \n");
					out.write(textPane_2.getText() + " \n");
					JCheckBox[] checkBoxes = { checkBox, checkBox_1,
							checkBox_2, checkBox_3, checkBox_4, checkBox_5,
							checkBox_6, checkBox_7, checkBox_8, checkBox_9 };
					for (int i = 0; i < checkBoxes.length; i++) {
						out.write(checkBoxes[i].isSelected() + " \n");
					}
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String file = editL + ", " + editF + ".txt";
				File la = new File("Patient/" + file);
				la.delete();
				File ba = new File("Patient/" + file);
				temp.renameTo(ba);
				clear();
			}
		}
	}
	/**
     * Clears all of the labels
     */
	void clear() {
		label_5.setText("Treatment Record for Appointment on: date");
		label_6.setText("Doctor: ");
		label_7.setText("Nurse: ");
		textField.setText(" ");
		textField_3.setText(" ");
		textField_1.setText(" ");
		textField_2.setText(" ");
		textPane.setText(" ");
		textPane_1.setText(" ");
		textPane_2.setText(" ");
		JCheckBox[] checkBoxes = { checkBox, checkBox_1, checkBox_2,
				checkBox_3, checkBox_4, checkBox_5, checkBox_6, checkBox_7,
				checkBox_8, checkBox_9 };
		for (int i = 0; i < checkBoxes.length; i++) {
			checkBoxes[i].setEnabled(false);
			checkBoxes[i].setSelected(false);
		}
		btnSaveOrders.setText("Edit Records & Orders");
		btnSaveOrders.setVisible(false);
		btnView.setText("View");
	}

}
